package com.shop.controller.user;

import com.shop.entity.ShopProduct;
import com.shop.entity.ShopUser;
import com.shop.service.ShopProductService;
import com.shop.service.ShopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private ShopProductService shopProductService;

    @PostMapping("/login")
    public String login(HttpSession session, String username, String password, Model model) {

        ShopUser shopUser = shopUserService.getShopUserByUsername(username);
        if(shopUser != null) {
            if(password.equals(shopUser.getUserPassword())){
                System.out.println("登录成功！");
                session.setAttribute("USER_LOGIN", username);
                return "redirect:/";
            }
            else {
                System.out.println("密码错误，登录失败！");
                return "redirect:login.html";
            }
        } else {
            System.out.println("用户不存在！");
            return "redirect:login.html";
        }
    }

    @GetMapping("/index")
    public String index(Model model) {
        List<ShopProduct> shopProductList = shopProductService.GetShopProductList();
        model.addAttribute("shopProducts", shopProductList);
        return "/index";
    }

}
